package com.txy.webmanagement.control;

import com.txy.webmanagement.model.SysUser;
import com.txy.webmanagement.service.SysUserService;
import com.txy.webmanagement.utils.AliSendSmsUtils;
import com.txy.webmanagement.utils.RequestDataUtils;
import com.txy.webmanagement.utils.StringUtils;
import com.txy.webmanagement.utils.SysParrentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 验证码
 */
@Controller
@RequestMapping(value = "verification")
public class VerificationControl {
    @Autowired
    private SysUserService userService;

    /**
     * 注册使用验证码
     *
     * @return
     */
    @RequestMapping(value = "vfcRegister")
    @ResponseBody

    public RequestDataUtils vfcRegister() {
        RequestDataUtils res = new RequestDataUtils();

        return res;
    }

    @RequestMapping(value = "vfcPsdReset")
    @ResponseBody
    public RequestDataUtils vfcPsdReset(HttpServletRequest request, String phonyNumber) {
        RequestDataUtils res = new RequestDataUtils();
        if (StringUtils.checkStringNotNull(phonyNumber)) {
            SysUser us = userService.getSysUserByUserName(phonyNumber.trim());
            if (us != null) {
                int rd6 = (int) ((Math.random() * 9 + 1) * 100000);
                String code = rd6 + "";
                //此处增加发送手机短信的接口
                Boolean boo = AliSendSmsUtils.sendSms(phonyNumber,code);
                if (boo) {
                    HttpSession httpSession = request.getSession();
                    String sessionId = httpSession.getId();
                    String vfcCode = SysParrentUtils.vfcPasCode(sessionId);
                    httpSession.setAttribute(vfcCode, code);
                    res.setCode(SysParrentUtils.successCode);
                    res.setMessage("发送成功!");
                } else {
                    res.setCode(SysParrentUtils.errorCode);
                    res.setMessage("发送失败!" + "");
                }

            } else {
                res.setCode(SysParrentUtils.errorCode);
                res.setMessage("账号不存在！！！!");
                return res;
            }
        } else {
            res.setCode(SysParrentUtils.errorCode);
            res.setMessage("发送成功!");
            return res;
        }
        return res;
    }
}
